#!/usr/bin/env python
# -*- coding:utf8 -*-

"""
@version: 
@author: lh
@license: Apache Licence 
@contact: liuhuan0672@gmail.com
@site: 
@software: PyCharm
@file: 0014.py
@time: 2016/1/21 15:15

第 0014 题： 纯文本文件 student.txt为学生信息, 里面的内容（包括花括号）如下所示：
{
    "1":["张三",150,120,100],
    "2":["李四",90,99,95],
    "3":["王五",60,66,68]
}
请将上述内容写到 student.xls 文件中。

"""

# tips: You want json.load for loading a file.
#         json.loads is for loading from a string.
#  tips: ValueError: '__init__' in __slots__ conflicts with class variable
#     打开Python33\Lib\site-packages\xlwt3\formula.py
#     将 ： __slots__ = ["__init__",  "__s", "__parser", "__sheet_refs", "__xcall_refs"]
#     修改为：__slots__ = [ "__s", "__parser", "__sheet_refs", "__xcall_refs"]

import xlwt3
import json


def write_txt_to_xls(txt_file):
    txt_obj = open(txt_file, 'r', encoding="utf-8")
    file_content = json.load(txt_obj)

    xls_object = xlwt3.Workbook(encoding="utf-8")
    sheet = xls_object.add_sheet('student')
    for i in range(len(file_content)):
        sheet.write(i, 0, i + 1)
        data = file_content[str(i + 1)]
        for j in range(len(data)):
            sheet.write(i, j + 1, data[j])
    xls_object.save("./result/student.xls")


if __name__ == '__main__':
    write_txt_to_xls("./static/students.txt")
